<?php
$script = 'survey';
require_once '../../controller.php';
require_once '../../header.php';
require_once '../../classes/Survey.php';
if(isset($_SESSION['logged_in'])) {
    
    $instructor_id = $_SESSION['logged_in'];

}


// Displaying Survey Results.
if(isset($_REQUEST['sid'])) {

    $survey_id = $_REQUEST['sid'];

    echo '<h1 align="center">Survey Results</h1>';

    $s = new Survey();
    $survey = $s->getSurvey($survey_id);

    echo '<p align="center"> Survey Name: '.$survey['name'].'</p><br>';
    
    $questions = $s->getQuestions($survey_id);
    
    if($questions){
    foreach ($questions as $i => $value) {
         $question_id[] = $value->id;
         $type[] = $value->type;
         $question_value[] = $value->question;
         $response1[] = $value->response1;
         $response2[] = $value->response2;
         $response3[] = $value->response3;
         $response4[] = $value->response4;
         $response5[] = $value->response5;
         $response6[] = $value->response6;
       }
    }
   }
    $num = sizeof($question_id);

    if(!isset($_POST['select_questions'])) {
    	
        echo '<form name="select_ques" method ="post" >';
        echo "<br>";
        echo "<input type='checkbox' id='check_all' onclick='javascript:checkAll($num);'  name='all' value='all'> View all questions <br><br>";
        echo "<b>(OR)</b><br><br><b>Select Questions to view</b>";
        echo "<p align='right'><a href='statistics.php?sid=$survey_id'><font color='blue' size='8em'><u>View Statistics</u></font></a></p>";
        echo "<br>";
        
        print "<div style='border:1px black solid; width:400px; height:300px; overflow:auto;'>";
        echo "<table>";
        for($i=0;$i<$num;$i++) {
        	if($type[$i]!='Section Text'){
            echo "<tr><td width='800px'><input type='checkbox' id='check_$i' name='que_$i' value='$question_id[$i]'>$question_value[$i]</td></tr>";
        }
        }
        echo "<tr><td>&nbsp;</td></tr>";
        print "</table>";
        print "</div><br>";
        echo '<input type="submit" name="select_questions" value="Select Questions"> <br><br>';
        echo '</form>';
        
    print "<div id='select-result-questions'>";
    print "<p><b>View Results:</b> Select all or particular questions to view results. results will be diaplayed in tabular format.</p><br>";
    print "<p><b>Select All:</b> Select All if you want to see results for all the questions.</p><br>";
    print "<p><b>View statistics:</b> Click on the link above to view survey results statistics. Stats are shown for specific type of questions and textual responses are displayed as it is. </p><br>";
    print "</div>";
        
    }

   
    if(isset($_POST['select_questions'])) {
    	echo "<p align='right'><a href='statistics.php?sid=$survey_id'><font color='blue' size='8em'><u>View Statistics</u></font></a></p>";
    	echo "<p align='right'><a href='export.php'><font color='blue' size='8em'><u>Export Results</u></font></a></p><br><br>";
        print "<div style='width:800px; height:400px; overflow:auto;'>";
    	echo '<table border="1px" cellpadding="10px">
        <tr><td>Group Name</td>
        <td>Student Name</td>
        <td>Date</td>
        <td>Time</td>';
        $datafile = "datafile.csv";
        $handle = fopen($datafile,'w') or die("can't open file");
        fwrite($handle,'Group Name,Respondent Name,Date,Time');
        $display_questions =0;
        for($i=0;$i<$num;$i++) {

            if(isset($_POST['que_'.$i])){
                $display_questions = $display_questions +1;
                $display_responses[]=$i;
                echo  '<td>'.($i+1).'. '.$question_value[$i].'</td>';
                fwrite($handle,','.$question_value[$i]);
        }
        }
        echo '</tr>';
        fwrite($handle,"\r\n");
          
        $responses = $s->getResponses($survey_id);
        if($responses){
        foreach ($responses as $r=>$value) {
             $rid[] = $value->id;
             $member_id[] = $value->student_id;
             $member_name[] = $value->student_name;
             $date[] = $value->timestamp;
               
            }

        }else {
            echo '<p align="center"><font size = "6.2em" color ="red">No Response Available yet</font></p><br><br>';

        }
    
        $size = sizeof($rid);
        $response = new Response();
        for($j=0;$j<$size;$j++) {

            $student = new Student($member_id[$j]);
            $group_id = $student ->group;
            
            $group = new Group($group_id);
            $group_name = $group->name;
           
            $datetime= explode(" ",$date[$j]);

            echo "<tr><td>".$group_name."</td><td>".$member_name[$j]."</td><td>".$datetime[0]."</td><td>".$datetime[1]."</td>";
            fwrite($handle, $group_name.','.$member_name[$j].','.$datetime[0].','.$datetime[1]);
            for($n =0;$n<$display_questions;$n++) {
            if(isset($_POST['que_'.$display_responses[$n]])) {
               $response_value = $response->getResponseValue($rid[$j],$question_id[$display_responses[$n]]);
               
               if($type[$display_responses[$n]]=='Check Box' ||$type[$display_responses[$n]]=='Rate'){
               	$saved_responses = explode("+",$response_value);
               	foreach($saved_responses as $new){
               		if($new!=""){
               			$new_responses[]=$new;
               		}
               	}
               	
               	$response_value= implode(";",$new_responses);
               	$new_responses= array();
               }
               
               if($type[$display_responses[$n]]=='Rate'){
               	$response_value_array = explode(";",$response_value);
               	foreach($response_value_array as $rv){
               		$rv_s = $rv;
               		$rv_seperate = explode("-",$rv_s);
               		if($rv_seperate[0]=='res1'){
               			$response_part1 = $response1[$display_responses[$n]];
               		}elseif($rv_seperate[0]=='res2'){
               			$response_part1 = $response2[$display_responses[$n]];
               		}elseif($rv_seperate[0]=='res3'){
               			$response_part1 = $response3[$display_responses[$n]];
               		}elseif($rv_seperate[0]=='res4'){
               			$response_part1 = $response4[$display_responses[$n]];
               		}elseif($rv_seperate[0]=='res5'){
               			$response_part1 = $response5[$display_responses[$n]];
               		}elseif($rv_seperate[0]=='res6'){
               			$response_part1 = $response6[$display_responses[$n]];
               		}
               		
               		$response_part2 = $rv_seperate[1];
               		$response_string = $response_part1."-->".$response_part2;
               		$rate_response[]=$response_string;
               	}
               	
               	$response_value = implode(";",$rate_response);
               	$rate_response = array();
               }
               
               echo "<td>".$response_value."</td>";
               fwrite($handle, ','.$response_value);
              }
             }  
            echo "</tr>";
            fwrite($handle, "\r\n");
        }
       
        echo '</table></div><br><br>';
        fclose($handle);

    }

   
require_once '../../footer.php';
?>



